package com.cms.admin.action.bill;

import com.cms.admin.action.BaseAction;
import com.cms.admin.core.http.bean.GetCashApplyReq;
import com.cms.admin.model.*;
import com.cms.admin.service.bill.IUserAccountVoucherBillService;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.stuff.StuffService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YYY-Work on 2017/5/25.
 */
public class UserAccountVoucherBillAction extends BaseAction implements ModelDriven<UserAccountVoucherBillExt> {

    private IUserAccountVoucherBillService userAccountVoucherBillService;
    private TemplateService templateService;
    private StuffService stuffService;
    private UserAccountVoucherBillExt search = new UserAccountVoucherBillExt();
    private PageBean page;

    public String requestUserAccountVoucherBill() {
        return SUCCESS;
    }

    public String ajaxQueryUserAccountVoucherBill() {

        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        page = new PageBean(search.getCurrentPage(), search.getPageSize());
        List<UserAccountVoucherBill> userAccountVoucherBills = userAccountVoucherBillService.queryVoucherBill(page, search);

        String res = null;
        try {
            res = this.getQueryResult(userAccountVoucherBills, page);

            JSONObject o = new JSONObject();

            o.put("pageCount", page.getTotalPage());
            o.put("content", res);

            return ajaxJsonSuccessMessage(o);
        } catch (Exception e) {
            LOG.error("ajaxQueryStuff", e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }

    }

    public String ajaxAgreeUserAccountVoucherBill() {
        HttpServletRequest request = ServletActionContext.getRequest();
        if (null != search.getUserid()) {
            String docId = String.valueOf(search.getUserid());
            Long userId = ((UserList) request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid();

            GetCashApplyReq req = new GetCashApplyReq();
            req.setDocId(docId);
            req.setUserId(String.valueOf(userId));
            req.setFlag("1");

            String resp = serverPostHead("getCashApply", req);
            return ajaxJson(resp);
        } else {

            JSONObject res = new JSONObject();
            res.put("errorCode", -1);
            res.put("errorMsg", -1);
            return ajaxJson(res);
        }
    }

    public String ajaxRefusedUserAccountVoucherBill() {
        HttpServletRequest request = ServletActionContext.getRequest();
        if (null != search.getUserid()) {
            String docId = String.valueOf(search.getUserid());
            Long userId = ((UserList) request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid();

            GetCashApplyReq req = new GetCashApplyReq();
            req.setDocId(docId);
            req.setUserId(String.valueOf(userId));
            req.setFlag("2");

            String resp = serverPostHead("getCashApply", req);
            return ajaxJson(resp);
        } else {

            JSONObject res = new JSONObject();
            res.put("errorCode", -1);
            res.put("errorMsg", -1);
            return ajaxJson(res);
        }
    }

    private String replaymessage;
    
    public String ajaxOneKeyForCash() {
        StuffList example = new StuffList();
        example.setTypeid(3);
        example.setIsUsedFlag((short) 0);
        List<StuffList> stuffLists = stuffService.selectStuffList(example);
        if (null != stuffLists) {
            for (StuffList stuffList : stuffLists) {
                GetCashApplyReq req = new GetCashApplyReq();
                req.setDocId(String.valueOf(stuffList.getStuffid()));
                serverPostHead("getCashApply", req);
            }
        }
        replaymessage ="提现成功!";
        return "success";
    }

    public String getReplaymessage() {
		return replaymessage;
	}

	public void setReplaymessage(String replaymessage) {
		this.replaymessage = replaymessage;
	}

	private String getQueryResult(List<UserAccountVoucherBill> stuffLists, PageBean page)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("voucherBillList", stuffLists);
        map.put("page", page);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_VOUCHERBILL_QUERY_RESULT, map);
        return result;
    }

    @Override
    public UserAccountVoucherBillExt getModel() {
        return search;
    }

    public IUserAccountVoucherBillService getUserAccountVoucherBillService() {
        return userAccountVoucherBillService;
    }

    public void setUserAccountVoucherBillService(IUserAccountVoucherBillService userAccountVoucherBillService) {
        this.userAccountVoucherBillService = userAccountVoucherBillService;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    public UserAccountVoucherBillExt getSearch() {
        return search;
    }

    public void setSearch(UserAccountVoucherBillExt search) {
        this.search = search;
    }

    public PageBean getPage() {
        return page;
    }

    public void setPage(PageBean page) {
        this.page = page;
    }

    public StuffService getStuffService() {
        return stuffService;
    }

    public void setStuffService(StuffService stuffService) {
        this.stuffService = stuffService;
    }
}
